package org.soarsky.wealthchengdu.google.map;


import org.soarsky.wealthchengdu.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class MarkOperator implements MarkOverlaysInterface {

	private Activity mContext = null;
	private AbstractGoogleMap mAbstractGoogleMap = null;
	private MapOverlaysControlFactory mControlFactory = null;
	private MarkOverlaysInterface mOverlaysInterface;
	private View popupLocation = null;
	private TextView mTitleView = null;
	private TextView mContentView = null;
	private Button mButton = null;
	private MapView mapView = null;
	private BasePoint currBasePoint = null;

	public MarkOperator(Activity context, AbstractGoogleMap abstractGoogleMap) {
		mContext = context;

		mAbstractGoogleMap = abstractGoogleMap;
		mControlFactory = mAbstractGoogleMap.getMapOverlaysControlFactory();
		mapView = mAbstractGoogleMap.getMapView();
		mOverlaysInterface = mControlFactory.getMarkOverlays();
		initView();
		initEven();
	}

	private void initEven() {
		// TODO Auto-generated method stub

		mOverlaysInterface.setOnTapCallBack(new OverlayCallBack() {

			@Override
			public void onLostFocus() {
				// TODO Auto-generated method stub
				if (popupLocation != null && popupLocation.isShown()) {
					popupLocation.setVisibility(View.GONE);
					mapView.invalidate();
					// mapControlData.setShowPopupDialog(false);
					currBasePoint = null;

				}
			}

			@Override
			public void onGotFocus(BasePoint basePoint) {
				// TODO Auto-generated method stub
				showPopupLocation((MarkPoint) basePoint);
				currBasePoint = basePoint;
			}
		});
	}

	private GeoPoint createGeoPoint(double latitude, double longitude) {
		return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
	}

	private void showPopupLocation(MarkPoint markPoint) {
		// if (loc == null) {

		// return;
		// }
		animateTo(markPoint.getId());
		if (popupLocation.getVisibility() != View.VISIBLE)
			popupLocation.setVisibility(View.VISIBLE);

		mTitleView.setText(markPoint.getTitle());
		// if(loc.getAddress()!=null&&!loc.getAddress().equals(""))
		mContentView.setText(markPoint.getContent());

		mapView.removeAllViews();
		mapView.addView(
				popupLocation,
				new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
						MapView.LayoutParams.WRAP_CONTENT, createGeoPoint(
								markPoint.getLatitude(),
								markPoint.getLongitude()), 0, -45,
						MapView.LayoutParams.BOTTOM_CENTER));

	}

	private void initView() {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popupLocation = inflater.inflate(R.layout.google_map_marker_popup, null);
		mContentView = (TextView) popupLocation.findViewById(R.id.mapsnippet);
		mTitleView = (TextView) popupLocation.findViewById(R.id.maptitle);
		mButton = (Button) popupLocation.findViewById(R.id.mapbut);
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				BDLocation loc= GoogleMapActivity.getInstance().getBaiduLocation().getLocation();
				Log.e("text", currBasePoint +"   "+loc);
				mAbstractGoogleMap.clearLines();
				if (currBasePoint != null&&loc!=null) {
					mAbstractGoogleMap.addLinePoint(0, currBasePoint.getLatitude(), currBasePoint.getLongitude());
					mAbstractGoogleMap.addLinePoint(1,loc.getLatitude(),loc.getLongitude());
					mAbstractGoogleMap.invalidate();
				} 
			}
		});
	}

	@Override
	public void addMarkOverlay(MarkPoint markOverlay) {
		// TODO Auto-generated method stub
		mOverlaysInterface.addMarkOverlay(markOverlay);
	}

	@Override
	public void setMarkOverlay(MarkPoint markOverlay) {
		// TODO Auto-generated method stub
		mOverlaysInterface.setMarkOverlay(markOverlay);
	}

	@Override
	public void setMarkOverlay(int id, Drawable icon) {
		// TODO Auto-generated method stub
		mOverlaysInterface.setMarkOverlay(id, icon);
	}

	@Override
	public MarkPoint getMarkOverlay(int id) {
		// TODO Auto-generated method stub
		return mOverlaysInterface.getMarkOverlay(id);
	}

	@Override
	public void deleteMarkOverlay(int id) {
		// TODO Auto-generated method stub
		mOverlaysInterface.deleteMarkOverlay(id);
	}

	@Override
	public void enableMarkAll() {
		// TODO Auto-generated method stub

	}

	@Override
	public void disableMarkAll() {
		// TODO Auto-generated method stub

	}

	@Override
	public void animateTo(int id) {
		// TODO Auto-generated method stub
		mOverlaysInterface.animateTo(id);
	}

	@Override
	public void animateTo(int id, int x, int y) {
		// TODO Auto-generated method stub
		mOverlaysInterface.animateTo(id, x, y);
	}

	@Override
	public void setCenter(int id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setCenter(int id, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnFocus(int id, boolean foucs) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnFocus(int id) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean getOnFocus() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setOnTapCallBack(OverlayCallBack callBack) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearMarkOVerlays() {
		// TODO Auto-generated method stub

	}

	@Override
	public void distory() {
		// TODO Auto-generated method stub

	}

}
